/** 下载 base64格式的文件 */
export function downloadBase64File(base64: string, filename: string) {
    const base64ToBlob = function (code: string) {
        const parts = code.split(';base64,')
        const contentType = parts[0].split(':')[1]
        const raw = window.atob(parts[1])
        const rawLength = raw.length
        const uInt8Array = new Uint8Array(rawLength)

        for (let i = 0; i < rawLength; ++i) {
            uInt8Array[i] = raw.charCodeAt(i)
        }
        return new Blob([uInt8Array], {
            type: contentType,
        })
    }

    const aLink = document.createElement('a')
    const blob = base64ToBlob(base64)
    const evt = document.createEvent('HTMLEvents')
    evt.initEvent('click', true, true)
    aLink.download = filename
    aLink.href = URL.createObjectURL(blob)
    aLink.click()
}
